<?php 
namespace app\common\service;

use think\Db;
/**
 * 奖励
 */
class Back{

	
	/**
	 * [register 激活奖励]
	 * @Author   思睿软件
	 * @DateTime 2021-07-13T16:41:53+0800
	 * @Version  1.0
	 * @param    array                    $user_ids [ 上级用户列表 越后越高]
	 * @param    float                    $base
	 */
	static function active($base,$user_ids,$table,$table_id){
		if(0==floatval($base) || !is_array($user_ids) || 0==count($user_ids)) return 0;
		$levels = model('UserLevel')->getAll();
		if(empty($levels)) return 0;
		$rs = self::getUsersByIds($user_ids);
		if(false===$rs) return 0;   
		$f1  = $rs[0];
		//奖励推荐人
		if($f1['level']==0) return 0;
		$level1 = get_one_by_item($levels,'level',$f1['level']);
		// $rate = $level['active_back_rate'];
		$base1 = $base * $level1['active_back_rate'] /100 ;
		self::doBack($f1, $base1,11,'激活奖励',$table,$table_id);
        
		//奖励上级
		$f2 = $rs[1];
		if($f2  && ($f2['level']> $f1['level']) ){
			$level2 = get_one_by_item($levels,'level',$f2['level']);
			$base2  = $base * ($level2['active_back_rate']-$level1['active_back_rate']) /100;
			if($base2>0){
				self::doBack($f2, $base2,11,'激活间接奖励',$table,$table_id);
			}
		}

		//奖励上上级
		$f3 = $rs[2];
		if($f3  && ($f3['level']> $f2['level']) ){
			$level3 = get_one_by_item($levels,'level',$f3['level']);
			$base3  = $base * ($level3['active_back_rate']-$level2['active_back_rate']) /100;
			if($base3>0){
				self::doBack($f3, $base3,11,'激活间接奖励',$table,$table_id);
			}
		}
		
		return 1;
	}





	//利润奖励
	static function  profit($base,$user_ids=array()){

		if(0==floatval($base) || !is_array($user_ids) || 0==count($user_ids)) return 0;
		$levels = model('UserLevel')->getAll();
		if(empty($levels)) return 0;
		$rs = self::getUsersByIds($user_ids);
		if(false===$rs) return 0;   
		$f1  = $rs[0];
		//奖励推荐人
		if($f1['level']==0) return 0;
		$level1 = get_one_by_item($levels,'level',$f1['level']);

		$base1 = $base * $level1['profit_back_rate'] /100 ;
		self::doBack($f1, $base1,12,'利润奖励',$table,$table_id);
        
		//奖励上级
		$f2 = $rs[1];
		if($f2  && ($f2['level']> $f1['level']) ){
			$level2 = get_one_by_item($levels,'level',$f2['level']);
			$base2  = $base * ($level2['profit_back_rate']-$level1['profit_back_rate']) /100;
			if($base2>0){
				self::doBack($f2, $base2,12,'利润间接奖励',$table,$table_id);
			}
		}

		//奖励上上级
		$f3 = $rs[2];
		if($f3  && ($f3['level']> $f2['level']) ){
			$level3 = get_one_by_item($levels,'level',$f3['level']);
			$base3  = $base * ($level3['profit_back_rate']-$level2['profit_back_rate']) /100;
			if($base3>0){
				self::doBack($f3, $base3,12,'利润间接奖励',$table,$table_id);
			}
		}
		
		return 1;
	}


	//只获取3级 奖励只做到3级
	static function  getUsersByIds($user_ids){
		$parent_ids = count($user_ids)>3? array_slice($user_ids, 0,3): $user_ids;
		$rows = Db::name('user')->where(['id'=>['in',$parent_ids]])->select()->toArray();
		if(empty($rows)) return [];
		return $rows;
	}



	static function  doBack($user,$amount,$type,$info,$table='',$table_id=0){
		if($amount==0) return 0;
		Db::startTrans();
		try {
			Db::name('user')->where('id',$user['id'])->update(['amount'=>Db::raw('amount + ' .$amount)]);
			Db::name('balance')
				->insert([
					'type'         =>$type,
					'user_id'      =>$user['id'],
					'amount'       =>$amount,
					'end_amount'   =>$user['amount'] + $amount,
					'create_at'    =>time(),
					'remark'       =>$info,
					'table'        =>$table,
					'table_id'     =>$table_id
				]);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
		}
		//修改账户
		//写入明细
		return 1;
	}



}